package com.ytt.core.service.utils;

import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Specifications;
import com.ytt.core.model.scores.ScoresType;
import com.ytt.core.service.service.ProposalQueryParam;
import org.springframework.stereotype.Component;

@Component
public class BaseUtil {

    /**
     * 根据条件生成PredicateBuilder，PredicateBuilder通过build()会生成Specification
     * @param param 查询条件
     * @param <T> 待查询实体的泛型
     * @return 返回PredicateBuilder，PredicateBuilder可以根据特定情况再增加条件
     */
    public static <T> PredicateBuilder<T> getBuilder(ProposalQueryParam param) {
        String name = param.getName();
        String student_id=param.getStudent_id();
        String schoolClass = param.getSchoolClass();
        String college = param.getCollege();
        String major = param.getMajor();
        ScoresType scoresType=param.getScoresType();
        int score=param.getScore();
        return Specifications.<T>and()
                .eq(name != null && !"".equals(name) && !"null".equals(name), "user.name", name)
//                .eq( schoolClass!= null && !"".equals(schoolClass) && !"null".equals(schoolClass), "studentId.user.schoolClass.name", schoolClass)
//                .eq( college!= null && !"".equals(college) && !"null".equals(college), "studentId.user.schoolClass.college.name", college)
//                .eq( major!= null && !"".equals(major) && !"null".equals(major), "studentId.major.name", major)
                .eq(scoresType != null, "scoresType", scoresType)
                .ge (true, "scores", score)
                .eq(student_id != null && !"".equals(student_id) && !"null".equals(student_id), "student_id", student_id);
    }
}
